#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{

    //设置固定大小
    this->setFixedSize(320, 588);

    //标题
    this->setWindowTitle("选择关卡");

    //图片
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单栏
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");

    //创建菜单项
    QAction * quitAction = startMenu->addAction("退出");

    //点击退出 实现退游戏
    connect(quitAction, &QAction::triggered, [=](){
       this->close();
    });

    //音效按钮
    QSound * chooseSound = new QSound(":/res/TapButtonSound.wav", this);
    QSound * backSound = new QSound(":/res/TapButtonSound.wav", this);

    //返回按钮创建
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");

    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(), this->height()-backBtn->height());

    connect(backBtn, &MyPushButton::clicked, [=](){
//        qDebug() << "返回主场景" ;

        //隐藏自身,发送信号. 信号槽
//        emit this->chooseSceneBack();

        backSound->play();

        QTimer::singleShot(50, this, [=]() {
           emit this->chooseSceneBack();
        });
    });


    playScene = NULL;



    //选择关卡按钮
    for (int i = 0; i < 20; i++) {
        MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + (i%4)*70, 130 + (i/4)*70);

        connect(menuBtn, &MyPushButton::clicked, [=](){
//            QString str = QString("您选择的是第 %1 关").arg(i+1);
//            qDebug()<< str;

            //播放音效
            chooseSound->play();

            //隐藏自身
            this->hide();

            //进入具体游戏场景
            playScene = new PlayScene(i+1);
            playScene->setGeometry(this->geometry());
            playScene->show();

            connect(playScene, &PlayScene::chooseSceneBack, [=](){
                //选择关卡场景显示,翻金币游戏场景隐藏
                playScene->close();
                this->setGeometry( playScene->geometry() );

                delete playScene;
                playScene = NULL;
                this->show();
            });

        });

        QLabel * label = new QLabel(this);
        //标签大小
        label->setFixedSize(menuBtn->width(), menuBtn->height());
        //显示内容
        label->setText(QString::number(i+1));
        label->move(25 + (i%4)*70, 130 + (i/4)*70);
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

        //设置鼠标穿透,否则点击文字，button的事件不会被触发
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{

    QPainter painter(this);

    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");

    //画图拉伸
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);


    //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width() - pix.width())*0.5, 30, pix.width(), pix.height(), pix);



}

void ChooseLevelScene::testAbc(QPaintEvent *e)
{

}

